﻿using System;
using System.Data;
using System.Net;
using System.Configuration;
using System.Web;
using System.Text;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace webQQservice
{
    public class CacheTimer
    {

        //const string logPath = @"cacheLog.txt";
        const string cacheKeyName = "myCacheTimer";//缓存的名字


         string rawUrl = ConfigurationManager.AppSettings["site"];//缓存过期时请求的页面地址

        public void RegistCacheTimmer()
        {
           
                if (HttpContext.Current.Cache.Get(cacheKeyName) != null) return;
                try
                {

                    HttpContext.Current.Cache.Add(cacheKeyName, '1', null,
                        DateTime.Now.AddSeconds(15), Cache.NoSlidingExpiration,
                        CacheItemPriority.High,
                        new CacheItemRemovedCallback(CacheItemOnRemoved));
                  
                }
                catch (Exception e)
                {
                    
                }
            
        
        }
        protected void CacheItemOnRemoved(string key, object value, CacheItemRemovedReason reason)
        {
           Global.log("缓存移除，回调！");
            if (key == cacheKeyName&&Global.login)
            {
                //QQHelp.SendMsg(Global.item.Cookie, Global.friendList["360902777"], DateTime.Now.ToString() + "缓存移除,执行刷新", Global.item.ClientId, Global.item.Psessionid);
                Global.log("QQ在线，刷新消息");
                Global.RefreshSession();
            } 
            RequestWebPage();
        }
        protected void RequestWebPage()
        {
            WebClient wc = new WebClient();
            Global.log("加载页面");
            wc.DownloadData(rawUrl);
            //CacheTimer registTimer = new CacheTimer();
            //registTimer.RegistCacheTimmer();
        }
    }
}